package com.ruoyi.web.controller.interview;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.HuaWeiSISUtils;
import com.ruoyi.common.utils.QiNiuUtils;
import com.ruoyi.interview.domain.InterviewAnswer;
import com.ruoyi.interview.service.IInterviewAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;

/**
 * 面试回答控制器
 */
@RestController
@RequestMapping("/interview/answer")
public class InterviewAnswerController {

    @Autowired
    IInterviewAnswerService interviewAnswerService;

    @PostMapping("/testUploadRecord")
    public AjaxResult testUploadRecord (MultipartFile file,Long questionId) throws IOException {
        System.out.println("questionId:"+questionId);
        String filename = file.getOriginalFilename();
        File target = new File("D:/files/" + filename);
        FileOutputStream outputStream = new FileOutputStream(target);
        InputStream inputStream = file.getInputStream();

        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
        outputStream.flush();
        outputStream.close();
        return  AjaxResult.success();
    }

    @PostMapping("/handleAnswer")
    public AjaxResult handelAnswer(MultipartFile file,Long questionId) throws IOException {
        //1.接受上传文件
            //文件名
            String filename = file.getOriginalFilename();
            //文件名的处理
            //上传到七牛云
            QiNiuUtils.upload(file.getInputStream(),filename);
            //把信息存储到mysql中
            InterviewAnswer answer = new InterviewAnswer();
            //设置值
            answer.setQuestionId(questionId);
            final String fileUrl =  Constants.QINIU_URL_PREFIX + filename;
            answer.setAudioUrl(fileUrl);
            interviewAnswerService.insertInterviewAnswer(answer);
            Long answerId = answer.getId();

            //调用华为云的SIS进行录音转文本
            //异步形式
            new Thread(()->{
                String jobId = HuaWeiSISUtils.doSubmit(fileUrl);
                Map<String, Object> map = HuaWeiSISUtils.getResult(jobId);
                //修改数据库中的Content 和 duration
                InterviewAnswer interviewAnswer = interviewAnswerService.selectInterviewAnswerById(answerId);
                Integer audioDuration = (Integer)map.get("audioDuration");
                interviewAnswer.setDuration(audioDuration.longValue());
                interviewAnswer.setContent((String) map.get("text"));
                interviewAnswerService.updateInterviewAnswer(interviewAnswer);
            }).start();
        return AjaxResult.success();
    }

    @PostMapping("/handleAnswerNoFile/{questionId}")
    public AjaxResult handelAnswer(@PathVariable Long questionId) throws IOException {
        //把信息存储到mysql中
        InterviewAnswer answer = new InterviewAnswer();
        //设置值
        answer.setQuestionId(questionId);
        // 无录音文件的情况
        System.out.println("无录音模式提交答案，questionId: " + questionId);
        answer.setAudioUrl(null);  // 没有音频文件
        answer.setContent("（无录音）");  // 标记为无录音
        answer.setDuration(0L);  // 时长为0
        interviewAnswerService.insertInterviewAnswer(answer);

        return AjaxResult.success();
    }
}
